package com.zbkj.service.service.adminregion.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.core.util.URLUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zbkj.common.model.merchant.Merchant;
import com.zbkj.common.page.CommonPage;
import com.zbkj.common.region.SearchRegionVo;
import com.zbkj.common.request.PageParamRequest;
import com.zbkj.common.request.merchant.MerchantSearchRequest;
import com.zbkj.common.response.MerchantHeaderNumResponse;
import com.zbkj.common.response.MerchantPageResponse;
import com.zbkj.common.utils.CrmebDateUtil;
import com.zbkj.common.vo.DateLimitUtilVo;
import com.zbkj.service.dao.MerchantDao;
import com.zbkj.service.helper.region.RegionHelper;
import com.zbkj.service.service.adminregion.IRegionMerchantService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @ClassName: RegionMerchantServiceImpl
 * @Description: TODO
 * @Author: fanbing
 * @Date: 2024/8/11 15:28
 * @Version: V1.0
 **/
@Service
@Slf4j
public class RegionMerchantServiceImpl implements IRegionMerchantService {

    @Autowired
    private RegionHelper regionHelper;

    @Autowired
    private MerchantDao merchantDao;

    @Override
    public PageInfo<MerchantPageResponse> getRegionPage(MerchantSearchRequest searchRequest, PageParamRequest pageParamRequest) {
        SearchRegionVo currentUserRegion = regionHelper.getCurrentUserRegion();
        if (currentUserRegion.isMerchantIdNullFlag()) {
            return new PageInfo<>();
        }
        Page<Merchant> page = PageHelper.startPage(pageParamRequest.getPage(), pageParamRequest.getLimit());
        LambdaQueryWrapper<Merchant> lqw = getRegionPageWrapperBySearch(searchRequest, currentUserRegion);
        lqw.orderByDesc(Merchant::getSort);
        List<Merchant> merchantList = merchantDao.selectList(lqw);
        if (CollUtil.isEmpty(merchantList)) {
            return CommonPage.copyPageInfo(page, CollUtil.newArrayList());
        }
        List<MerchantPageResponse> responseList = merchantList.stream().map(e -> {
            MerchantPageResponse response = new MerchantPageResponse();
            BeanUtils.copyProperties(e, response);
            return response;
        }).collect(Collectors.toList());
        return CommonPage.copyPageInfo(page, responseList);
    }

    private LambdaQueryWrapper<Merchant> getRegionPageWrapperBySearch(MerchantSearchRequest searchRequest, SearchRegionVo currentUserRegion) {
        LambdaQueryWrapper<Merchant> lqw = Wrappers.lambdaQuery();
        lqw.in(Merchant::getId, currentUserRegion.getMerchantIds());
        if (ObjectUtil.isNotNull(searchRequest.getCategoryId())) {
            lqw.eq(Merchant::getCategoryId, searchRequest.getCategoryId());
        }
        if (ObjectUtil.isNotNull(searchRequest.getTypeId())) {
            lqw.eq(Merchant::getTypeId, searchRequest.getTypeId());
        }
        if (ObjectUtil.isNotNull(searchRequest.getIsSelf())) {
            lqw.eq(Merchant::getIsSelf, searchRequest.getIsSelf());
        }
        if (ObjectUtil.isNotNull(searchRequest.getIsSwitch())) {
            lqw.eq(Merchant::getIsSwitch, searchRequest.getIsSwitch());
        }
        if (StrUtil.isNotBlank(searchRequest.getKeywords())) {
            String keywords = URLUtil.decode(searchRequest.getKeywords());
            lqw.and(i -> i.like(Merchant::getName, keywords)
//                    .or().apply(StrUtil.format(" find_in_set('{}', keywords)", keywords)));
                    .or().apply(" find_in_set({0}, keywords)", keywords));
        }
        if (StrUtil.isNotBlank(searchRequest.getDateLimit())) {
            DateLimitUtilVo dateLimitUtilVo = CrmebDateUtil.getDateLimit(searchRequest.getDateLimit());
            lqw.between(Merchant::getCreateTime, dateLimitUtilVo.getStartTime(), dateLimitUtilVo.getEndTime());
        }
        lqw.eq(Merchant::getIsDel, false);
        return lqw;
    }

    /**
     * 商户分页列表表头数量
     *
     * @return MerchantHeaderNumResponse
     */
    @Override
    public MerchantHeaderNumResponse getListHeaderNum(MerchantSearchRequest searchRequest) {
        SearchRegionVo currentUserRegion = regionHelper.getCurrentUserRegion();
        if (currentUserRegion.isMerchantIdNullFlag()) {
            return new MerchantHeaderNumResponse();
        }

        //开店
        LambdaQueryWrapper<Merchant> lqw = getRegionPageWrapperBySearch(searchRequest, currentUserRegion);
        lqw.eq(Merchant::getIsSwitch, 1);
        Integer openNum = merchantDao.selectCount(lqw);

        // 关闭的商户数
        LambdaQueryWrapper<Merchant> queryWrapper = getRegionPageWrapperBySearch(searchRequest, currentUserRegion);
        queryWrapper.eq(Merchant::getIsSwitch, 0);
        Integer closeNum = merchantDao.selectCount(queryWrapper);
        return new MerchantHeaderNumResponse(openNum, closeNum);
    }
}
